package lumingweihua.future.cn.lumingweihua.information;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.zhusx.core.adapter.Lib_BaseRecyclerAdapter;
import com.zhusx.core.interfaces.IHttpResult;
import com.zhusx.core.network.HttpRequest;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import lumingweihua.future.cn.lumingweihua.R;
import lumingweihua.future.cn.lumingweihua.network.LoadData;
import lumingweihua.future.cn.lumingweihua.network.LoadingHelper;
import lumingweihua.future.cn.lumingweihua.network.SimpleHttpListener;
import lumingweihua.future.cn.lumingweihua.ui.BaseAppFragment;
import lumingweihua.future.cn.lumingweihua.ui.message.InfoDetailActivity;
import lumingweihua.future.cn.lumingweihua.ui.message.MessageActivity;
import lumingweihua.future.cn.lumingweihua.ui.message.domain.CountEntity;
import lumingweihua.future.cn.lumingweihua.ui.message.domain.InfoData;
import lumingweihua.future.cn.lumingweihua.widget.FrescoImageView;

/**
 * Created by Administrator on 2018/3/2.
 */

public class InformationFragment extends BaseAppFragment {
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.iv_read)
    View ivRead;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_information, container, false);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        ButterKnife.bind(this, view);
        LoadData<List<InfoData>> loadData = new LoadData<>(LoadData.Api.资讯列表, this);
        loadData._setOnLoadingListener(new LoadingHelper<List<InfoData>>(recyclerView, loadData) {
            @Override
            public void __onComplete(HttpRequest<Object> request, IHttpResult<List<InfoData>> data) {
                initView(data.getData());
            }
        });
        loadData._loadData();
        LoadData<CountEntity> countData = new LoadData<>(LoadData.Api.系统通知数量, this);
        countData._setOnLoadingListener(new SimpleHttpListener<CountEntity>() {
            @Override
            public void onLoadComplete(LoadData.Api api, HttpRequest<Object> request, IHttpResult<CountEntity> t) {
                if (!"0".equals(t.getData().message_num) || !"0".equals(t.getData().carriage_num)) {
                    ivRead.setVisibility(View.VISIBLE);
                }
            }
        });
        countData._loadData();
    }

    private void initView(List<InfoData> data) {
        recyclerView.setAdapter(new Lib_BaseRecyclerAdapter<InfoData>(data) {

            @Override
            protected void __bindViewHolder(_ViewHolder viewHolder, int i, final InfoData s) {
                viewHolder.setText(R.id.tv_message, s.title);
                viewHolder.setText(R.id.tv_date, s.create_time);
                viewHolder.<FrescoImageView>getView(R.id.iv_image).setImageURI(s.img);
                viewHolder.rootView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        startActivity(new Intent(view.getContext(), InfoDetailActivity.class).putExtra(_EXTRA_String_ID, s.id));
                    }
                });
            }

            @Override
            protected int __getLayoutResource(int i) {
                if (i == 1) {
                    return R.layout.item_list_information_1;
                }
                return R.layout.item_list_information_2;
            }

            @Override
            public int getItemViewType(int position) {
                if (TextUtils.isEmpty(getListData().get(position).video)) {
                    return 0;
                }
                return 1;
            }
        });
    }

    @OnClick({R.id.tv_titleBar_right})
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_titleBar_right:
                startActivity(new Intent(v.getContext(), MessageActivity.class));
                break;
        }
    }
}
